/**
 * UB-RIA-UI 1.0
 * Copyright 2014 Baidu Inc. All rights reserved.
 *
 * @file 将`不限` + 展开的`Region` / `RichSelector` / `TextLine`等控件进行组合
 * @exports RadioPanel
 * @author maoquan(maoquan@baidu.com)
 */
define(
    function (require) {

        var eoo = require('eoo');
        var InputControl = require('esui/InputControl');
        var esui = require('esui');
        var painters = require('esui/painters');
        var u = require('underscore');
        var lib = require('esui/lib');

        require('esui/BoxGroup');

        var TPL = require('text!./tpl/radiopanel.tpl.html');

        /**
         * 展开控件的类型以及配置由使用者通过模板自行定义
         *
         * ```
         * <div
         *   data-ui-type="RadioPanel"
         *   data-ui-id="radioPanelText"
         *   data-ui-name="name">
         *   <esui-region data-ui-name="region"></esui-region>
         * </div>
         *
         * ```
         *
         * @class RadioPanel
         * @extends esui.InputControl
         */
        var RadioPanel = eoo.create(
            InputControl,
            {

                /**
                 * @override
                 */
                type: 'RadioPanel',

                getCategory: function () {
                    return 'input';
                },

                /**
                 * @override
                 */
                initOptions: function (options) {
                    var properties = {
                        boxClass: 'ui-radio-custom',
                        otherText: '请选择',
                        allText: '不限',
                        allValue: 'ALL'
                    };
                    u.extend(properties, options);

                    // 应用场景为：第一层选项为`不限`和`请选择`（互斥），其中点`请选择`,展开子项，子项可多选
                    // 这里要为自动生成的其他选项添加一个id，以便在子项展开，获取控件值时使用
                    properties.otherValue = lib.getGUID('OTHER');

                    this.$super([properties]);
                },

                /**
                 * @override
                 */
                initStructure: function () {
                    var boxId = this.helper.getId('box');
                    var panelId = this.otherValue;
                    var html = lib.format(
                        TPL,
                        {
                            boxId: boxId,
                            boxClass: this.boxClass,
                            // 按`Radiotab`的约定命名，以保证panel的正常切换
                            panelId: panelId
                        }
                    );
                    $(this.main).prepend(html);

                    var properties = {};
                    properties[boxId] = {
                        datasource: [
                            {
                                text: this.allText,
                                value: this.allValue
                            },
                            {
                                text: this.otherText,
                                value: this.otherValue,
                                panel: this.otherValue
                            }
                        ]
                    };
                    // 分别是box、richControl
                    var controls = this.helper.initChildren(this.main, {properties: properties});
                    // 加到预设的panel容器中
                    var richControl = controls[1];
                    richControl.appendTo(lib.g(panelId));
                    // 相当于给child加个childName
                    this.addChild(richControl, 'rich-control');
                },

                /**
                 * @override
                 */
                initEvents: function () {
                },

                /**
                 * @override
                 */
                repaint: painters.createRepaint(
                    InputControl.prototype.repaint,
                    {
                        name: 'rawValue',
                        paint: function (me, rawValue) {
                            if (!rawValue || rawValue.length === 0) {
                                rawValue = [me.allValue];
                            }
                            if (rawValue[0] === me.allValue) {
                                me.getBoxControl().setProperties({rawValue: rawValue});
                            }
                            // 如果值不为['ALL'],则rawValue为具体控件的值，这时候设置box值为other
                            else {
                                me.getBoxControl().setProperties({rawValue: [me.otherValue]});
                                me.getRichControl().setProperties({rawValue: rawValue});
                            }
                        }
                    }
                ),

                getBoxControl: function () {
                    return this.getChild('box');
                },

                getRichControl: function () {
                    return this.getChild('rich-control');
                },

                getRawValue: function () {
                    var boxControl = this.getBoxControl();
                    var rawValue = boxControl.getRawValue();
                    if (rawValue[0] === this.allValue) {
                        return rawValue;
                    }
                    return this.getRichControl().getRawValue();
                },

                parseValue: function (value) {
                    if (u.isString(value)) {
                        return value.split(',');
                    }
                    return [];
                },

                stringifyValue: function (rawValue) {
                    return rawValue.join(',');
                },

                /**
                 * 进行验证
                 *
                 * @return {boolean}
                 */
                validate: function () {
                    var boxControl = this.getBoxControl();
                    var rawValue = boxControl.getRawValue();
                    var richControl = this.getRichControl();
                    if (rawValue[0] === this.otherValue) {
                        if (u.isFunction(richControl.validate)) {
                            return richControl.validate();
                        }
                    }
                    return true;
                }
            }
        );

        esui.register(RadioPanel);
        return RadioPanel;
    }
);
